package com.renyi.timer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.model.MybatisServiceDoctor;
import com.renyi.model.Question;
import com.renyi.model.QuestionExtra;
import com.renyi.util.CalendarUtil;
import com.renyi.util.Constants;
import com.renyi.util.JSONUtils;


public class QuestionThread implements Runnable {
	private static final Logger logger = org.slf4j.LoggerFactory
			.getLogger(QuestionThread.class);

	private MybatisServiceDoctor mybatisServiceDoctor;
	private TaskExecutor taskExecutor;
	private int sleepTimeOut = 1 * 60 * 1000;
	private HashMap<String, Object> pMap = new HashMap<String, Object>();

	private String jsondata = null;
	
	public QuestionThread(TaskExecutor taskExecutor,MybatisServiceDoctor mybatisServiceDoctor) {
		// TODO Auto-generated constructor stub
		this.taskExecutor = taskExecutor;
		this.mybatisServiceDoctor = mybatisServiceDoctor;
	}

	public void run() {
		// TODO Auto-generated method stub
		try {
			while (true) {
				Map<String, Object> pMap = new HashMap<String, Object>();
				Iterator iter = Constants.questionkHm.entrySet().iterator();
				while (iter.hasNext()) {
					Map.Entry entry = (Map.Entry) iter.next();
//					Object key = entry.getKey();
					QuestionExtra questionExtra = (QuestionExtra) entry.getValue();
					if (questionExtra.getIsDeal()==1) {
						pMap.put("id", questionExtra.getId());
						pMap.put("doctorID", questionExtra.getDoctorID());
						pMap.put("dealTime", questionExtra.getDealTime());
						int key = this.mybatisServiceDoctor.updateIbatisObject(
								"updateQuestionSQL", pMap);
						if (key > 0) {
							Constants.questionkHm.remove(questionExtra.getId());
						} 
						
					}
				}
				try {
					List<HashMap<String, Object>> list = this.mybatisServiceDoctor
							.findMybatisList("getQuestionListSQL", pMap);
					jsondata = JSON.toJSON(list).toString();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				TypeToken<ArrayList<QuestionExtra>> token =new TypeToken<ArrayList<QuestionExtra>>(){};
				List<QuestionExtra> questionList =  JSONUtils
						.fromJson(jsondata, token);
				
				if (questionList.size()<1) {
					System.out.println("问题列表为空.....");
				}
				for(int i=0;i<questionList.size();i++){
					QuestionExtra question = questionList.get(i);
					if(question!=null){
						Constants.questionkHm.put(String.valueOf(question.getId()), question);
					}
				}
				Thread.sleep(sleepTimeOut);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			taskExecutor.execute(new QuestionThread(taskExecutor,mybatisServiceDoctor));
		}
	}
	


}
